<?php
/**
 * Created by PhpStorm.
 * User: mww
 * Date: 2018/12/17
 * Time: 16:08
 */
namespace app\api\controller;
use think\Db;
class Selfcarps extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        header('Access-Control-Allow-Origin:*');
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }


    /**
     * 加入购物车
     */
    public function putGoodsIncar()
    {
        extract(input());
        $can = $this->getSonByUser($user_id);
        if($can['status'] != 200){
            return json($can);
        }
        $sondata = $can['msg'];
        if(isset($nowyear) && $nowyear){
            $sondata['uson_year'] = $nowyear;
        }

        $insize = $this->getInSizeForGood($good_id,$attr_id,$sondata);
        if($insize == false){
            return json(['status'=>0,'msg'=>'请重新选择尺码']);
        }

        $today = date('Y-m-d H:i:s');
        $data = [
            'user_id' => $user_id,
            'car_type' => 2,
            'car_user_son_id' => $sondata['uson_id'],
            'goods_id' => $good_id,
            'attr_id' => $attr_id, // 就是尺码数：150之类的
            'car_good_year' => $sondata['uson_year'],
            'num' => $num,
            'created_at' => $today,
            'updated_at' => $today,
        ];
        //是否相同
        $have = Db::table('up_self_car')
            ->where('car_user_son_id',$sondata['uson_id'])
            ->where('goods_id',$good_id)
            ->where('attr_id',$attr_id)
            ->where('car_good_year',$sondata['uson_year'])
            ->where('car_type',2)
            ->find();
        if($have){
            $res = Db::table('up_self_car')->where('id',$have['id'])->setInc('num',1);
        }else{
            $res = Db::table('up_self_car')
                ->insert($data);
        }

        if($res){
            return json(['status'=>200,'msg'=>'ok']);
        }else{
            return json(['status'=>0,'msg'=>'加入购物车失败，请刷新重试']);
        }
    }
    /**
     * 我的购物车
     */
    public function mycar()
    {
        extract(input());
        $can = $this->getSonByUser($user_id);
        if($can['status'] != 200){
            return json($can);
        }
        $sondata = $can['msg'];
        //这是这个用户所有的可以下单的校服
        $codes = $this->getStudentGoods($sondata['uson_school'],$sondata['uson_year'],$sondata['user_gender'],'','',2);
        //-------------------end-----
        $field = 'id,num,good_id,good_name,good_price price,good_thumb,good_color,car_good_year';
        $field .= ',good_status,attr_id,good_coding';
        $field .= ',good_90,good_100,good_110,good_120,good_130,good_140,good_150,good_155,good_160,good_165,good_170,good_175,good_180,good_185,good_190,good_195,good_200';
        $cars = Db::table('up_self_car')->alias('c')
            ->join('up_goods g','c.goods_id=g.good_id','LEFT')
            ->where('car_user_son_id',$sondata['uson_id'])
            ->where('car_type',2)
            ->field($field)
            ->order('updated_at DESC')
            ->page($page,10)
            ->select();
        if($cars){
            foreach ($cars as $k=>$v){
                $cars[$k]['stock'] = 1000;
                $cars[$k]['sign_str'] = $v["good_color"] . '/' . $v['attr_id'];
                $good_sta = $v['good_status'];
                if($good_sta == 1 && in_array($v['good_coding'],$codes)){
                    Db::table('up_self_car')->where('id',$v['id'])->setField('status',1);
                }else{
                    //如果你之前增补的衣服不在了，则默认为下架
                    Db::table('up_self_car')->where('id',$v['id'])->setField('status',4);
                }
                $cars[$k]['price'] = getGoodsRealPrice($v["good_coding"],$v['car_good_year'],$sondata);
            }
        }
        return json(['status'=>200,'msg'=>$cars]);
    }

    /**
     * 提交订单，查看订单详情（立即购买展示）
     * 其实要根据运费来计算
     * address_id：传就给这个地址，不传给默认的地址
     */
    public function sureOrder()
    {
        extract(input());
        $can = $this->getSonByUser($user_id);
        if($can['status'] != 200){
            return json($can);
        }
        $sondata = $can['msg'];
        if(isset($nowyear) && $nowyear){
            $sondata['uson_year'] = $nowyear;
        }
        $insize = $this->getInSizeForGood($good_id,$attr_id,$sondata);
        if($insize == false){
            return json(['status'=>0,'msg'=>'请重新选择尺码']);
        }
        $data = $this->lijigoumaips($good_id,$attr_id,$num,$sondata);
        if($data['status'] != 200){
            return json($data);
        }else{
            $good = $data['msg'];
        }
        //默认收货地址
        if(!isset($address_id) || empty($address_id)){
            $address_id = '';
        }
        $address = $this->myAddressInfo($user_id,$address_id);
        if(empty($address)){
            $address = '';
        }
        $good['sanwei'] = 1;
        if($good['good_pinming'] == '西服' || $good['good_pinming'] == '西服外套') $good['sanwei'] = 2;
        return json(['status'=>200,'msg'=>$good,'address'=>$address,'sondata'=>$sondata]);
    }
}